<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- <meta http-equiv="refresh" content="2" > -->
    <style>
        .container {
            width: 600px;
            height: 200px;
            overflow-y: auto;
            background-color: lightgray;
            margin-bottom: 10px;
        }
        .user {
            font-size: 22px;
            color: red;
            margin: 0 2px;
        }
        .time {
            font-size: 12px;
        }
        .msg {
            font-size: 18px;
            color: rgb(0, 183, 255);
        }
        .mine {
            text-align: right;
        }
        .input-msg {
            height: 30px;
            line-height: 30px;
            width: 400px;
        }
        .input-btn {
            width: 100px;
            height: 35px;
        }
    </style>
    <title>WBS20121聊天室</title>
</head>
<body>
    <h2>WBS20121聊天室</h2>
    <?php
        $loginUser = $_SESSION["loginUser"];
    ?>
    <h3>欢迎: <?=$loginUser["nickname"]?> (自己手动刷新获取最新的消息)</h3>
    <div class="container" >
        <?php
        $conn = mysqli_connect("localhost:3306","root","","php");
        $sql = "select m.id, m.msg, m.send_user_id userId, m.send_date sdate, u.nickname from t_msg m"
        . " inner join t_user u on u.id = m.send_user_id "
        . " order by m.send_date";
        $result = mysqli_query($conn, $sql);
        if($result){
            while($row = mysqli_fetch_assoc($result)){
                if($row['userId'] == $loginUser['id']){
                    echo "<p class=\"mine\" >";
                    echo "<span class=\"msg\" >{$row['msg']}</span>";
                    echo "<span class=\"user\" >:<span class=\"time\" >({$row['sdate']})</span>{$row['nickname']}</span>";
                    echo "</p>";
                } else {
                    echo "<p class=\"other\" >";
                    echo "<span class=\"user\" >{$row['nickname']}<span class=\"time\" >({$row['sdate']})</span>:</span>";
                    echo "<span class=\"msg\" >{$row['msg']}</span>";
                    echo "</p>";
                }
            }
        }
        mysqli_close($conn);
        ?>
        <!-- <p class="other" >
            <span class="user" >tom1<span class="time" >(2021-03-05 15:39:26)</span>:</span>
            <span class="msg" >hello world11111</span>
        </p>
        <p class="other" >
            <span class="user" >tom2<span class="time" >(2021-03-05 15:39:26)</span>:</span>
            <span class="msg" >hello world22222</span>
        </p>
        <p class="other" >
            <span class="user" >tom1<span class="time" >(2021-03-05 15:39:26)</span>:</span>
            <span class="msg" >hello world333333</span>
        </p>
        <p class="other" >
            <span class="user" >tom2<span class="time" >(2021-03-05 15:39:26)</span>:</span>
            <span class="msg" >hello world444444</span>
        </p>
        <p class="other" >
            <span class="user" >tom2<span class="time" >(2021-03-05 15:39:26)</span>:</span>
            <span class="msg" >hello world5555</span>
        </p>
        <p class="mine" >
            <span class="msg" >hello world5555</span>
            <span class="user" >:<span class="time" >(2021-03-05 15:39:26)</span>tom</span>
        </p>
        <p class="other" >
            <span class="user" >tom2<span class="time" >(2021-03-05 15:39:26)</span>:</span>
            <span class="msg" >hello world666</span>
        </p> -->

    </div>
    <script>
        let c = document.querySelector(".container");
        c.scrollTo(0, c.scrollHeight);
    </script>
    <div>
        <form action="/week_homework/doSendMsg.php" method="post">
            <input class="input-msg" type="text" name="msg" placeholder="请输入消息" >
            <input class="input-btn" type="submit" value="发送">
        </form>
    </div>
</body>
</html>